# -*- coding: utf-8 -*-
import math
from googlemaps1 import GoogleMaps

# from origin to destination
# 返回值是时间估计值(int)
# tranport: h避免高速公路, t避免收费路段, r采用公共交通, w步行方式, b骑行方式
# chooseMinumTime: bool类型，是否选择最小时间
# //underCurrentRoadCondition: bool类型，是否选择当前路况
def estimateTime(origin, destination, transport, chooseMinumTime):
    api_key = 'ABQIAAAAevysxt9O5lBUCrSalm80MxQx8gmx0K-_Fjj4Tf8bNXH3BBSxZRRmI_CuZM2zQyuXEpG_uxt-aqPr-A'
    gmaps = GoogleMaps(api_key)
    minTime = 0
    if ( transport=='h' or transport=='t' ):
        minTime = estimateTimeDrive(gmaps, origin, destination)
    else:
        timeList = gmaps.directionsTimeList(origin, destination, transport)
        # parse and filter query result
        minTime = timeList[0]
        for time in timeList:
            if time<minTime:
                minTime = time   
        #print minTime    
    return minTime

def estimateTimeDrive(gmaps, orgin, destination):
    directions = gmaps.directions(orgin, destination)
    if directions['Status']['code']==200:
        time = directions['Directions']['Duration']['seconds']
    else:
        time = 600
    return int(math.ceil(time/60))
    